/*********************************************************************
*                                                                    *
*   mask_3d -- creates a mask to use with sdf                        *
*                                                                    *
* Parameters:                                                        *
*       inst   -- char with instructions ("OPEN", "PUT", or "CLOSE") *
*       plot   -- struct with ploting data (defined in gen2d.h)      *
*       grid   -- struct with grid data (definied in gen2d.h)        *
*       fields -- pointer to field_array with data                   *
*                                                                    *
* Returns: pointer to file array                                     *
*                                                                    *
*********************************************************************/


#include "first_macro_3d.h"  /* Where global parameters are defined */
#include "structs_3d.h"      /* Where structures are defined */
#include "gen_3d.h"
#include "java_ser.h"
#include "java_fser.h"
#ifdef NETCDF
#include "netcdf.h"
#endif
#ifdef SDF
#include "sdf.h"
#endif


/* ----------------------------------------------------------------------------------------------------------------- */

/* --------------------------------------------- adisco_sdf ------------------------------------------------------*/

/* ----------------------------------------------------------------------------------------------------------------- */


#ifdef SDF

struct plot_3d *mask_3d(char inst,
			     struct PLOT_PAR *plot_ptr,
			     struct GRID_PAR *gri,
			     union fields *fields)
{
  char name[50];         /* used to store part of the plot title. */
  char cnames[50];
  char deltanames[50];
  int ret;
  int dim = 3;
  int dim_vector[3];
  double time;


/* number of gridpoints, data from struct plot */


 int npp_1 = (*plot_ptr).grid_plot_pts_1;
 int npp_2 = (*plot_ptr).grid_plot_pts_2;
 int npp_3 = (*plot_ptr).grid_plot_pts_3;

/* double time = (double)(*fields).a.time; */

/* number of variables, data from struct plot */

const int n_plots = (*plot_ptr).n_plots;

    strcpy(cnames, "x|y|z");
    strcpy(deltanames, "/nx,ny,nz/");

dim_vector[0] = npp_1;
dim_vector[1] = npp_2;
dim_vector[2] = npp_3;


#ifdef DEBUG
printf("write coordinate values in struct plot (SDF) \n");
#endif

/* form coordinate values for plot */

{
double x_i = (double)(*plot_ptr).initial_x;
double x_f = (double)(*plot_ptr).final_x;
double y_i = (double)(*plot_ptr).initial_y;
double y_f = (double)(*plot_ptr).final_y;
double z_i = (double)(*plot_ptr).initial_z;
double z_f = (double)(*plot_ptr).final_z;



register int l;
       for (l = 0; l < npp_1; l++){
(*plot_ptr).coordinate_values[l] = x_i + (double)l/(double)(npp_1-1)*(x_f-x_i);}

       for (l = 0; l < npp_2; l++){
(*plot_ptr).coordinate_values[npp_1+l] = y_i + (double)l/(double)(npp_2-1)*(y_f-y_i);}

       for (l = 0 ; l < npp_3; l++){
(*plot_ptr).coordinate_values[npp_1 + npp_2 + l] = y_i + (double)l/(double)(npp_3-1)*(z_f-z_i);}
}


#ifdef DEBUG
   printf("writing data in sdf files \n");
#endif

   time = (double)(*fields).a.time;

       {register int p_ind3;
       register int p_ind2;
       register int p_ind1;
       for(p_ind3 = 0; p_ind3 < npp_3; p_ind3++){
	   for (p_ind2 = 0; p_ind2 < npp_2; p_ind2++){
	       for(p_ind1 = 0 ; p_ind1 < npp_1; p_ind1++){
		   (*plot_ptr).plot_field[field_ind][p_ind3][p_ind2][p_ind1] =
		       (double)(*fields).a.u[(*plot_ptr).pointers[field_ind]][p_ind1*(*plot_ptr).factor_1][p_ind2*(*plot_ptr).factor_2][p_ind3*(*plot_ptr).factor_3];
	       }
	   }
       }
       }

       strcat(name, &(*plot_ptr).window_name[field_ind][0]);
 

	ret=gft_out_full(name,time,&dim_vector[0],cnames,dim,
                              &((*plot_ptr).coordinate_values[0]),&((*plot_ptr).plot_field[field_ind][0][0][0]));
 }
 }

#ifdef DEBUG
printf("finishing writing data in sdf files \n");
#endif
 }
   break;
 default:
     {
	 printf("Unknown command in first argument of adisco\n");
	 exit(0);
	 break;
     }
 }
return(plot_ptr);
 }

#endif

